package zhuyun.code.launcheractivity.view

import android.os.Bundle
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import kotlinx.android.synthetic.main.activity_guide_page.*
import zhuyun.code.launcheractivity.R
import zhuyun.code.launcheractivity.model.Constants
import zhuyun.code.launcheractivity.utils.SPUtils
import zhuyun.code.launcheractivity.view.adapter.SplashPagerAdapter
import zhuyun.code.launcheractivity.view.fragment.GuideFourFragment
import zhuyun.code.launcheractivity.view.fragment.GuideOneFragment
import zhuyun.code.launcheractivity.view.fragment.GuideThreeFragment
import zhuyun.code.launcheractivity.view.fragment.GuideTwoFragment


class GuidePageActivity : BaseActivity(),ViewPager.OnPageChangeListener {
    private var distance:Int = 0
    private lateinit var selectedView:ImageView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_guide_page)
        SPUtils.putBoolean(Constants.IS_FIRST_IN,false)
        val fragments = mutableListOf<Fragment>()
        fragments.add(GuideOneFragment())
        fragments.add(GuideTwoFragment())
        fragments.add(GuideThreeFragment())
        fragments.add(GuideFourFragment())
        val splashPagerAdapter =
            SplashPagerAdapter(
                fragments,
                supportFragmentManager
            )
        viewPager.adapter = splashPagerAdapter
        viewPager.setOnPageChangeListener(this)
        viewPager.currentItem = 0
        for(i in 0 until fragments.size){
            if(fragments.size >= 2){
                val indicatorImage = ImageView(this)
                val layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT)
                indicatorImage.setImageResource(R.drawable.shape_full_white)
                if(i!=0){
                    layoutParams.leftMargin = resources.getDimensionPixelSize(R.dimen.x5)
                }
                llIndicator.addView(indicatorImage,layoutParams)
            }

        }
        llIndicator.viewTreeObserver.addOnGlobalLayoutListener {
            distance = llIndicator.getChildAt(1).left - llIndicator.getChildAt(0).left
        }
        if (fragments.size >= 2){
            selectedView = ImageView(this)
            selectedView.setImageResource(R.drawable.shape_full_selected)
            val layoutParams = FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT)
            rootIndicator.addView(selectedView,layoutParams)
        }




    }

    override fun onPageScrollStateChanged(state: Int) {

    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
        val layoutParams:FrameLayout.LayoutParams = selectedView.layoutParams as FrameLayout.LayoutParams
        layoutParams.leftMargin = (distance*(position+positionOffset)).toInt()
        selectedView.layoutParams = layoutParams
    }

    override fun onPageSelected(position: Int) {

    }
}
